package com.jhws.business.customer.controller;

import com.jhws.business.customer.bo.CustConstants;
import com.jhws.business.customer.bo.PayCustomer;
import com.jhws.business.customer.bo.PayCustomerSecret;
import com.jhws.business.customer.service.PayCustService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.common.pay.bo.PayConstants;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.DateUtil;
import com.jhws.util.jsonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer/pay")
public class PayCustCtrl {

    @Resource
    PayCustService payCustService;
    @Resource
    ITblUserDimService IUserService;

    @RequestMapping("listPage.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData listPage(){
        return new JsonRespData("",1, payCustService.findPayCustomersPage());
    }

    @RequestMapping("list.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData list(){
        return new JsonRespData("",1, payCustService.findPayCustomers());
    }

    @RequestMapping("listCustSecrets.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData listCustSecrets(Long id){
        return new JsonRespData("",1, payCustService.findPayCustomerSecrets(id));
    }

    /**
     * 获取Web用户绑定的支付组列表
     * @return
     */
    @RequestMapping("queryCustByUser.x")
    @ResponseBody
    public JsonRespData queryCustByUser(){
        TblUserDimBo user = IUserService.currentUser();
        List<PayCustomer> list = payCustService.findPayCustByWhere(" creator = '"+user.getLogName()+"'");
        return new JsonRespData("",1,list);
    }

    @RequestMapping("saveSecret.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData saveSecret(PayCustomerSecret customer){
        payCustService.save(customer);
        return new JsonRespData("",1,"");
    }

    /**
     * 获取Web用户绑定的支付参数列表
     * @return
     */
    @RequestMapping("querySecretByUser")
    @ResponseBody
    public JsonRespData querySecretByUser(){
        TblUserDimBo user = IUserService.currentUser();
        List listWx  = payCustService.findCustomerSecretsByWhere(" creator = '"+user.getLogName()+"' and type = '"+PayConstants.PAY_TYPE_WEIXIN_H5+"'");
        List listAli = payCustService.findCustomerSecretsByWhere(" creator = '"+user.getLogName()+"' and type = '"+PayConstants.PAY_TYPE_ALIPAY+"'");
        Map retMap = new HashMap();
        retMap.put("wx",listWx);
        retMap.put("ali",listAli);
        return new JsonRespData("",1,retMap);
    }

    /**
     * 非admin用户调用，自己提交支付参数，由admin审核分配
     * @param customer
     * @return
     */
    @RequestMapping("saveSecretByUser.x")
    @ResponseBody
    public JsonRespData saveSecretByUser(PayCustomerSecret customer){
        if(!SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)){
            TblUserDimBo user = IUserService.currentUser();
            PayCustomer payCustomer = payCustService.getPayCustomerByAccount(user.getLogName());
            if(payCustomer == null){
                payCustomer = payCustService.saveNewCustomForUser(user);
            }
            customer.setCustomId(payCustomer.getId());
            customer.setCreator(user.getLogName());
            payCustService.save(customer);
            return new JsonRespData("",1,"");
        }else
            return new JsonRespData("",-1,"您无权访问此接口!");
    }

    @RequestMapping("saveAlipayAccount.x")
    @ResponseBody
    public JsonRespData saveAlipayAccount(String alipayAccount){
        if(!SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)){
            TblUserDimBo user = IUserService.currentUser();

            PayCustomerSecret secret = new PayCustomerSecret();
            secret.setCreator(user.getLogName());
            secret.setMchId(alipayAccount);
            secret.setType(PayConstants.PAY_TYPE_ALIPAY);
            secret.setState(CustConstants.PAY_SECRET_STATE_APPLY);
            secret.setTime(DateUtil.getTimeNowFmtSec());
            payCustService.save(secret);
            return new JsonRespData("",1,"");
        }else
            return new JsonRespData("",-1,"您无权访问此接口!");
    }

    @RequestMapping("saveSecretState.x")
    @ResponseBody
    public JsonRespData saveSecretState(Long id,Integer state){
        PayCustomerSecret secret = payCustService.getPayCustSecretById(id);
        if(secret == null)
            return new JsonRespData("",-1,"未找到此条记录");

        secret.setState(state);
        payCustService.save(secret);
        return new JsonRespData("",1,"");
    }

    /**
     * 找到所有客户提交的支付宝申请代签的账户
     * @return
     */
    @RequestMapping("findAlipayApplying.x")
    @ResponseBody
    public JsonRespData findAlipayApplying(){
        return new JsonRespData("",1,payCustService.findCustomersSecretsApplyingPage());
    }

    @RequestMapping("save.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData save(PayCustomer customer){
        payCustService.save(customer);
        return new JsonRespData("",1,"");
    }

    /**
     * 保存Web用户录入的支付参数
     * @param customName
     * @param secretsJson
     * @return
     */
    @RequestMapping("saveNewCustomerWithSecrets.x")
    @ResponseBody
    public JsonRespData saveNewCustomerWithSecrets(String customName, String secretsJson){
        List<PayCustomerSecret> secrets = jsonUtil.getBeansFromJsonArray(secretsJson,PayCustomerSecret.class);
        TblUserDimBo user = IUserService.currentUser();
        PayCustomer payCustomer = new PayCustomer();
        payCustomer.setCustomName(user.getLogName());
        payCustomer.setCustomDetail(customName);
        payCustomer.setCreator(user.getLogName());
        payCustService.save(payCustomer);

        for(PayCustomerSecret secret : secrets){
            secret.setCustomId(payCustomer.getId());
            payCustService.save(secret);
        }

        return new JsonRespData("",1,"");
    }

    @RequestMapping("delete.x")
    @RequiresRoles("admin")
    @ResponseBody
    public JsonRespData delete(String id){
        payCustService.delete(Long.valueOf(id));
        payCustService.deleteDetailByCustomId(Long.valueOf(id));
        return new JsonRespData("",1,"");
    }

    /**
     * 非admin账号删除自己的支付分组
     * @param id
     * @return
     */
    @RequestMapping("deleteCustomerForUser.x")
    @ResponseBody
    public JsonRespData deleteCustomerForUser(Long id){
        payCustService.delete(id);
        return new JsonRespData("",1,"");
    }

    @RequestMapping("deleteDetail.x")
    @ResponseBody
    public JsonRespData deleteDetail(String id){
        if(SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN))
            payCustService.deleteDetail(Long.valueOf(id));
        else{
            PayCustomerSecret secret = payCustService.getPayCustSecretById(Long.valueOf(id));
            TblUserDimBo user = IUserService.currentUser();
            if(user.getLogName().equals(secret.getCreator())){
                payCustService.deleteDetail(Long.valueOf(id));
            }else
                return new JsonRespData("",-1,"您无权删除此数据!");
        }
        return new JsonRespData("",1,"");
    }

}
